//
//  YAYIResumeCell.m
//  多点执业
//
//  Created by hxp on 16/6/22.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIResumeCell.h"
#import "Masonry.h"
//#define YAYIColor(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

#define YAYIFont(t) [UIFont systemFontOfSize:t]

@implementation YAYIResumeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatViews];
    }
    return self;
}

-(void)creatViews{
    self.bgView = [UIView new];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
    self.dotLabel = [UILabel new];
    self.dotLabel.frame = CGRectMake(0, 10, 4, 30);
    self.dotLabel.backgroundColor = [UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1];
    [self.bgView addSubview:self.dotLabel];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.textColor = YAYIColor(83,165, 243);
    self.titleLabel.font = YAYIFont(14);
    [self.bgView addSubview:self.titleLabel];
    
    self.line = [UILabel new];
    self.line.backgroundColor = [UIColor lightGrayColor];
    _line.alpha = 0.4;
    [self.bgView addSubview:self.line];
    
    self.textView = [UITextView new];
    self.textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textView.layer.borderWidth = 0.5;
    self.textView.delegate = self;
    [self.bgView addSubview:self.textView];
    
    self.placeholder = [UILabel new];
    self.placeholder.font = [UIFont systemFontOfSize:14];
    self.placeholder.textColor = [UIColor lightGrayColor];
    self.placeholder.textAlignment = NSTextAlignmentLeft;
    self.placeholder.numberOfLines = 0;
    [self.bgView addSubview:self.placeholder];
    
    self.count = [UILabel new];
    self.count.font = [UIFont systemFontOfSize:14];
    self.count.textColor = [UIColor lightGrayColor];
    self.count.textAlignment = NSTextAlignmentRight;
    self.count.text = @"0/300";
    [self.bgView addSubview:self.count];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.mas_equalTo(@10);
        make.size.mas_equalTo(CGSizeMake(width, 220));
    }];
    [self.dotLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@4);
        make.size.mas_equalTo(CGSizeMake(4, 30));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dotLabel).with.offset(15);
        make.top.mas_equalTo(self.dotLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(100,30));
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(4);
        make.size.mas_equalTo(CGSizeMake(width, 0.3));
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@4);
        make.top.mas_equalTo(self.line.mas_bottom).with.offset(8);
        make.size.mas_equalTo(CGSizeMake(width - 8, 160));
    }];
    
    [self.placeholder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.textView).offset(5);
        make.top.mas_equalTo(self.textView.mas_top).offset(6);
        make.right.mas_equalTo(self.textView.mas_right).with.offset(-4);
    }];
    [self.count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.textView.mas_right).with.offset(-4);
        make.bottom.mas_equalTo(self.textView.mas_bottom).with.offset(-4);
        make.size.mas_equalTo(CGSizeMake(180, 10));
    }];
}
-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        self.placeholder.hidden = YES;
        
    }else{
        self.placeholder.hidden = NO;
    }
    
    if (textView.text.length <= 301) {
        self.count.text = [NSString stringWithFormat:@"%ld/%ld",textView.text.length,(300 - textView.text.length)];
    }else{
        textView.text =[textView.text substringWithRange:NSMakeRange(0, 300)];
        return;
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (_delegate && [_delegate respondsToSelector:@selector(index: content:)]) {
        [_delegate index:_index content:textView.text];
    }
}
@end
